
package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.entity.PersonDetail.Person;
import com.banzhi.shop.model.CallBack.PersonDetailCallBack;
import com.banzhi.shop.model.IPersonDetailModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


/**
 * Created by HT on 2017/5/3.
 * 用户信息
 */

public class PersonDetailImpl implements IPersonDetailModel {

    @Override
    public void ChangeData(String avatar, String nickname, String birthyear, String birthmonth, String birthday, String gender, final PersonDetailCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.editUserInfoUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        if (ASUtils.isCanUse(avatar)) {
            params.addBodyParameter("avatar", avatar);
        }
        if (nickname != null) {
            params.addBodyParameter("nickname", nickname);
        }
        if (birthyear != null) {
            params.addBodyParameter("birthyear", birthyear);
        }
        if (birthmonth != null) {
            params.addBodyParameter("birthmonth", birthmonth);
        }
        if (birthday != null) {
            params.addBodyParameter("birthday", birthday);
        }
        if (gender != null) {
            params.addBodyParameter("gender", gender);
        }
        LogUtils.d("PersonDetailImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        callBack.onChangeDataSuccess(jsonobject);
                    } else {
                        callBack.onChangeDataError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("PersonDetailImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(38)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    @Override
    public void getData(final PersonDetailCallBack callBack) {
        final RequestParams params = new RequestParams(URLString.hostUrl + URLString.getUserInfoUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("PersonDetailImpl2", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        Person person = gson.fromJson(result, Person.class);
                        callBack.ongetDataSuccess(person.getUserInfo());
                    } else {
                        callBack.ongetDataError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("PersonDetailImpl2", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }
}
